/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.sac.bc;

import edu.upn.sigemfi.sac.be.Documento;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class DocumentoDelegate {

    private DocumentoFacadeLocal local;

    public DocumentoDelegate() {
        local = lookupDocumentoFacade();
    }

    private DocumentoFacadeLocal lookupDocumentoFacade() {
        try {
            Context c = new InitialContext();
            return (DocumentoFacadeLocal) c.lookup("java:comp/env/DocumentoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public void crear(Documento doc) throws Exception{
        try {
            local.create(doc);
        } catch (Exception ex){
            local = lookupDocumentoFacade();
            throw new Exception("Crear Documento... " + ex.getMessage());
        }
    }

    public void editar(Documento doc) throws Exception{
        try {
            local.edit(doc);
        } catch (Exception ex){
            local = lookupDocumentoFacade();
            throw new Exception("Editar Documento... " + ex.getMessage());
        }
    }

    public void eliminar(Documento par) throws Exception{
        try {
            local.remove(par);
        } catch (Exception ex){
            local = lookupDocumentoFacade();
            throw new Exception("Eliminar Documento... " + ex.getMessage());
        }
    }

    public List<Documento> todos() throws Exception{
        List<Documento> all = null;

        try {
            all = local.findAll();
        } catch (Exception ex){
            local = lookupDocumentoFacade();
            throw new Exception("Listar Documentos... " + ex.getMessage());
        }

        return all;
    }


}
